/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle csq-3@163.com
 * @since : 2015-05-18 23:12:28
 * @filename : src/router/router.js
 * @version : 
 * @description : 
 */
 
define(function(require, exports, module) {
   
    var moduleTA = require('{publicUrl}/components/moduleTA/moduleTA'); 

    var router = Backbone.Router.extend({
        
        /**
         *  moduleName :　默认：index
         *  json : {
                    //根据业务，定制参数
                    type : (module transition animations) 默认：default(显隐动画）, slider(左右滑动）等其他过度动画,只针对模块单元 //系统级的
                    data : {} //用户自定义参数                
            }
         *  
         */ 
        prevObj : null,       
 
        urlJson: {
            type : 'slide',
            data : {}
        },
         
        initialize: function(options) {
            var tthis = this;
            this.route(/.*/, "all", function(){tthis.resolveUrl('index','')});
            this.route(":name", "module", function(name){tthis.resolveUrl(name)});
            this.route(":name/:str", "module", function(name,str){tthis.resolveUrl(name,str)});
        },

        resolveUrl: function(moduleName,str) {
            var paramObj = {};
            if (str) {
                try {
                    var obj = JSON.parse(str);
                    paramObj = _.clone(_.extend(this.urlJson,obj));
                } catch(e) {
                    paramObj = _.clone(this.urlJson);
                    //throw "Invalid Json Params";
                }
            } else {
                paramObj = _.clone(this.urlJson);
            }
            paramObj.moduleName = moduleName;
            if (this.prevObj === null) {
               paramObj.index = true;
            }
            this.dispatcher(paramObj); 
        },

        dispatcher: function(paramObj) {
            if ($Global.cacheModule[paramObj.moduleName]) {
                $Global.cacheModule[paramObj.moduleName].render(paramObj);
                this.actionModuleTA(paramObj);   
            } else {
                this.moduleLoad(paramObj);
            }
        },

        moduleLoad : function(paramObj) {
            var tthis = this;
                require.async(['{controller}/'+paramObj.moduleName],function(module) {
                    if (module) {
                        tthis.moduleLoaded(paramObj,module);
                    } else {
                        $Share.makeError("Invalid Module!");
                    }
                });
        },     

        moduleLoaded : function(paramObj,module) {
            $Global.currentModule = paramObj.moduleName;
            var instance = new module();
            instance.render(paramObj); 
            $Global.cacheModule[paramObj.moduleName] = instance;     
            this.actionModuleTA(paramObj);   
        },

        actionModuleTA : function(paramObj) {
            $('#moduleBox').css('overflow','hidden');//动画在没有滚动条的时候，不会出线白色边线．会在动画next完成后恢复滚动条的．
            moduleTA.action(this.prevObj,'current');
            moduleTA.action(paramObj,'next');
            this.prevObj = _.clone(paramObj);
        },
    }); 
    
    module.exports = router;   
});

